package com.imooc.miaosha.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.imooc.miaosha.common.Const;
import com.imooc.miaosha.dao.GoodsMapper;
import com.imooc.miaosha.dao.MiaoshaGoodsMapper;
import com.imooc.miaosha.dto.GoodsDTO;
import com.imooc.miaosha.dto.GoodsManageVO;
import com.imooc.miaosha.dto.GoodsVO;
import com.imooc.miaosha.dto.SecondKillDetailVO;
import com.imooc.miaosha.pojo.Goods;
import com.imooc.miaosha.pojo.MiaoshaGoods;
import com.imooc.miaosha.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author zxk
 */
@Slf4j
@Service
public class GoodServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    MiaoshaGoodsMapper miaoshaGoodsMapper;

    @Override
    public List<GoodsVO> list() {

        List<GoodsVO> GoodsVOList = Lists.newArrayList();
        List<MiaoshaGoods> miaoshaGoodsList = miaoshaGoodsMapper.getMiaoShaGoodByGoodsId(null);
        for (MiaoshaGoods miaoshaGoods : miaoshaGoodsList) {
            Goods goods = goodsMapper.selectByPrimaryKey(miaoshaGoods.getGoodsId());
            GoodsVO GoodsVO = adapt(goods, miaoshaGoods);
            GoodsVOList.add(GoodsVO);

        }
        return GoodsVOList;
    }

    public int getTotalCount() {
        return goodsMapper.countGoods();
    }

    @Override
    public List<GoodsManageVO> manageList() {
        List<Goods> goodsList = goodsMapper.listGoods();
        List<MiaoshaGoods> miaoshaGoodsList = miaoshaGoodsMapper.getMiaoShaGoodByGoodsId(null);
        Set<Long> secKillIdSet = Sets.newHashSet();
        miaoshaGoodsList.forEach(miaoshaGoods -> {
            secKillIdSet.add(miaoshaGoods.getId());
        });
        List<GoodsManageVO> goodsManageVOList = Lists.newArrayList();
        goodsList.forEach(goods -> {
            GoodsManageVO goodsManageVO = new GoodsManageVO();
            BeanUtils.copyProperties(goods, goodsManageVO);
            boolean isSecKill = this._isExistInSecKillIdSet(goods.getId(), secKillIdSet);
            goodsManageVO.setHasSecondKill(isSecKill);
            goodsManageVOList.add(goodsManageVO);
        });
        return goodsManageVOList;
    }

    @Override
    public SecondKillDetailVO getDetail(Long id) {
        MiaoshaGoods miaoshaGoods = miaoshaGoodsMapper.selectByPrimaryKey(id);
        if (miaoshaGoods == null) {
            return null;
        }
        return adaptDetail(miaoshaGoods);
    }

    @Override
    public List<GoodsVO> searchByIdList(List<Integer> idList) {
        List<Goods> goodsList = goodsMapper.selectByIdList(idList);
        List<GoodsVO> goodsVOList = Lists.newArrayList();
        goodsList.forEach(goods -> {
            GoodsVO goodsVO = new GoodsVO();
            BeanUtils.copyProperties(goods, goodsVO);
            goodsVOList.add(goodsVO);
        });
        return goodsVOList;
    }

    @Override
    public Map<String, Object> add(GoodsDTO goodsDTO) {
        System.out.println(goodsDTO);
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsDTO, goods);
        goods.setGoodsTitle(goodsDTO.getGoodsName());
        int effectCount = goodsMapper.insert(goods);
        Long incrId = goods.getId();
        log.info(String.valueOf(incrId));
        Map<String, Object> respMap = Maps.newHashMap();
        if (effectCount == 0) {
            respMap.put("code", Const.STATUS_CODE.FAIL_CODE);
            respMap.put("msg", "新增失败");
        } else {
            respMap.put("code", Const.STATUS_CODE.SUCCESS_CODE);
            respMap.put("incrId", incrId);
            respMap.put("msg", "新增成功");
        }
        return respMap;
    }

    public GoodsVO adapt(Goods goods, MiaoshaGoods miaoshaGoods) {
        GoodsVO goodsVO = new GoodsVO();
        if (goods.getGoodsDetail() == null) {
            goods.setGoodsDetail("");
        }
        BeanUtils.copyProperties(goods, goodsVO);
        goodsVO.setMiaoshaPrice(miaoshaGoods.getMiaoshaPrice());
        goodsVO.setStartDate(miaoshaGoods.getStartDate());
        goodsVO.setMiaoshaId(miaoshaGoods.getId());
        goodsVO.setMisoshaStock(miaoshaGoods.getMisoshaStock());
        goodsVO.setEndDate(miaoshaGoods.getEndDate());
        return goodsVO;
    }

    public SecondKillDetailVO adaptDetail(MiaoshaGoods miaoshaGoods) {
        SecondKillDetailVO secondKillDetailVO = new SecondKillDetailVO();
        secondKillDetailVO.setSecondKillPrice(miaoshaGoods.getMiaoshaPrice());
        Goods goods = goodsMapper.selectByPrimaryKey(miaoshaGoods.getGoodsId());
        secondKillDetailVO.setGoodsName(goods.getGoodsName());
        secondKillDetailVO.setGoodPrice(goods.getGoodsPrice());
        secondKillDetailVO.setGoodsImg(goods.getGoodsImg());
        secondKillDetailVO.setStartTime(miaoshaGoods.getStartDate());
        secondKillDetailVO.setDetailId(miaoshaGoods.getId());
        secondKillDetailVO.setEndTime(miaoshaGoods.getEndDate());
        secondKillDetailVO.setStock(miaoshaGoods.getMisoshaStock());
        Long remainStartSeconds = (miaoshaGoods.getStartDate().getTime() - System.currentTimeMillis()) / 1000;
        Long remainEndTime = (miaoshaGoods.getEndDate().getTime() - System.currentTimeMillis()) / 1000;
        if (remainStartSeconds > 0) {
            secondKillDetailVO.setSecondKillStatus(Const.SECOND_KILL_STATUS.NO_START);
            secondKillDetailVO.setRemainSeconds(remainStartSeconds);
        } else if (remainStartSeconds < 0 && remainEndTime > 0) {
            secondKillDetailVO.setSecondKillStatus(Const.SECOND_KILL_STATUS.ACTING);
            secondKillDetailVO.setRemainSeconds(remainEndTime);
        } else {
            secondKillDetailVO.setSecondKillStatus(Const.SECOND_KILL_STATUS.FINISHED);
        }
        return secondKillDetailVO;
    }

    private boolean _isExistInSecKillIdSet(Long id, Set<Long> secKillIdSet) {
        for (Long sid : secKillIdSet) {
            if (sid.equals(id)) {
                return true;
            }
        }
        return false;
    }
}
